package com.bgs.eat.songMenu.service.impl;

import com.bgs.eat.config.CommonResult;
import com.bgs.eat.fileManager.service.FileService;
import com.bgs.eat.page.MyPageHelper;
import com.bgs.eat.page.PageRequest;
import com.bgs.eat.page.PageResult;
import com.bgs.eat.songMenu.entity.SongMenuEntity;
import com.bgs.eat.songMenu.entity.SongMenuToTypesEntity;
import com.bgs.eat.songMenu.mapper.SongMenuMapper;
import com.bgs.eat.songMenu.mapper.SongMenuToTypesMapper;
import com.bgs.eat.songMenu.service.SongMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class SongMenuServiceImpl implements SongMenuService {

    @Autowired
    SongMenuMapper songMenuMapper;

    @Autowired
    SongMenuToTypesMapper songMenuToTypesMapper;

    @Autowired
    FileService fileService;

    /**
     * 新增歌单 （创建歌单） （门户展示）
     * lzy 2022-9-5 09点24分
     * @param songMenuEntity
     * @return
     */
    @Override
    public CommonResult saveSongMenu(SongMenuEntity songMenuEntity) {
        songMenuEntity.setDelFlag(1);
        songMenuEntity.setMusicNum(0);
        songMenuEntity.setCoverId(340);
        songMenuEntity.setPlayNum(0);
        // 隐私默认 所有人可见
        songMenuEntity.setPrivacy(1);
        songMenuEntity.setCreateTime(new Date());
        songMenuEntity.setUpdateTime(new Date());
        int insert = songMenuMapper.insert(songMenuEntity);
        if (insert > 0) {
            return CommonResult.ok(insert);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
     * 获取歌单信息列表
     * lzy 2022-9-5 09点53分
     * @param creatorId
     * @return
     */
    @Override
    public CommonResult getSongMenuList(Integer creatorId) {
        SongMenuEntity songMenuEntity = new SongMenuEntity();
        songMenuEntity.setCreatorId(creatorId);
        songMenuEntity.setDelFlag(1);
        List<SongMenuEntity> songMenuList = songMenuMapper.select(songMenuEntity);
        return CommonResult.ok(songMenuList);
    }

    /**
     * 删除歌单（门户展示）
     * lzy 2022-9-5 11点38分
     * @param id
     * @return
     */
    @Override
    public CommonResult deleteSongMenu(Integer id) {
        SongMenuEntity songMenuEntity = new SongMenuEntity();
        songMenuEntity.setDelFlag(0);
        songMenuEntity.setDelTime(new Date());
        songMenuEntity.setId(id);
        int i = songMenuMapper.updateByPrimaryKeySelective(songMenuEntity);
        if (i > 0) {
            return CommonResult.ok(i);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
     * 获取被删除的表单（门户展示）
     * lzy 2022-9-5 14点58分
     * @param creatorId
     * @return
     */
    @Override
    public CommonResult getSongMenuListByDel(Integer creatorId) {
        SongMenuEntity songMenuEntity = new SongMenuEntity();
        songMenuEntity.setDelFlag(0);
        songMenuEntity.setCreatorId(creatorId);
        List<SongMenuEntity> songMenuList = songMenuMapper.select(songMenuEntity);
        return CommonResult.ok(songMenuList);
    }

    /**
     * 批量恢复歌单 （门户展示）
     * lzy 2022-9-5 15点33分
     * @param ids
     * @return
     */
    @Override
    public CommonResult renewSongMenus(Integer[] ids) {
        SongMenuEntity songMenuEntity = new SongMenuEntity();
        songMenuEntity.setDelFlag(1);
        for (Integer id : ids) {
            songMenuEntity.setId(id);
            songMenuMapper.updateByPrimaryKeySelective(songMenuEntity);
        }
        return CommonResult.ok();
    }

    /**
     * 批量彻底删除歌单 （门户展示）
     * lzy 2022-9-5 15点33分
     * @param ids
     * @return
     */
    @Override
    public CommonResult delSongMenus(Integer[] ids) {
        SongMenuEntity songMenuEntity = new SongMenuEntity();
        for (Integer id : ids) {
            songMenuEntity.setId(id);
            songMenuMapper.delete(songMenuEntity);
        }
        return CommonResult.ok();
    }

    /**
     * 歌单查重（门户展示）
     * lzy 2022-9-5 16点02分
     * @param songMenuName
     * @param creatorId
     * @return
     */
    @Override
    public CommonResult checkSongMenuName(String songMenuName,Integer creatorId) {
        SongMenuEntity songMenuEntity = new SongMenuEntity();
        songMenuEntity.setSongMenuName(songMenuName);
        songMenuEntity.setCreatorId(creatorId);
        int i = songMenuMapper.selectCount(songMenuEntity);
        if(i > 0){
            return CommonResult.error("500");
        } else {
            return CommonResult.ok(i);
        }
    }

    /**
     * 歌单信息查询（门户展示）
     * fys 2022-8-6 09点00分
     * @param id
     * @return
     */
    @Override
    public CommonResult getSongMenuDetails(Integer id) {
        SongMenuEntity songMenuEntity = new SongMenuEntity();
        songMenuEntity.setId(id);
        return CommonResult.ok(songMenuMapper.selectOne(songMenuEntity));
    }

    /**
     * 修改歌单信息
     * lzy 2022-9-6 14点30分
     * @param songMenuEntity
     * @param multipartRequest
     * @return
     */
    @Override
    public CommonResult editSongMenu(SongMenuEntity songMenuEntity, MultipartRequest multipartRequest) {
        MultipartFile file = multipartRequest.getFile("file");
        if (file != null) {
            Integer coverId = fileService.uploadFile(multipartRequest);
            songMenuEntity.setCoverId(coverId);
        }
        songMenuEntity.setUpdateTime(new Date());
        int i = songMenuMapper.updateByPrimaryKeySelective(songMenuEntity);
        String types = songMenuEntity.getTypes();
        Integer id = songMenuEntity.getId();
        SongMenuToTypesEntity songMenuToTypesEntity = new SongMenuToTypesEntity();
        songMenuToTypesEntity.setSId(id);
        songMenuToTypesMapper.delete(songMenuToTypesEntity);
        if (types != null && !types.equals("")) {
            String[] split = types.split(",");
            for (String s : split) {
                songMenuToTypesEntity.setTId(Integer.valueOf(s));
                songMenuToTypesMapper.insert(songMenuToTypesEntity);
            }
        }
        if (i > 0) {
            return CommonResult.ok(i);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
     * 获取推荐歌单列表信息 （门户展示）
     * lzy 2022-9-7 08点51分
     * @param pageRequest
     * @param typeId
     * @return
     */
    @Override
    public CommonResult getSongMenuRecommend(PageRequest pageRequest,Integer typeId) {
        SongMenuToTypesEntity songMenuToTypesEntity = new SongMenuToTypesEntity();
        if (typeId != 0) {
            songMenuToTypesEntity.setTId(typeId);
            List<SongMenuToTypesEntity> select = songMenuToTypesMapper.select(songMenuToTypesEntity);
            MyPageHelper.startPage(pageRequest);
            Example example = new Example(SongMenuEntity.class);
            Example.Criteria criteria = example.createCriteria();
            for (SongMenuToTypesEntity menuToTypesEntity : select) {
                Integer sId = menuToTypesEntity.getSId();
                criteria.orEqualTo("id",sId);
            }
            List<SongMenuEntity> songMenuEntities = songMenuMapper.selectByExample(example);
            PageResult pageResult = MyPageHelper.pageInfo(songMenuEntities);
            return CommonResult.ok(pageResult);
        } else {
            return CommonResult.ok();
        }
    }

    /**
     * 根据用户id查询歌单名称
     * hbx 2022年9月8日11:34:18
     * @param creatorId
     * @return
     */
    @Override
    public List<SongMenuEntity> getSongMenuNameByCreatorId(Integer creatorId) {
        Example example = new Example(SongMenuEntity.class);
        example.createCriteria().andEqualTo("creatorId",creatorId);
        List<SongMenuEntity> select = songMenuMapper.selectByExample(example);
        return select;
    }

    /**
     * 根据歌单id查询歌单名称
     * hbx 2022年9月14日09:17:21
     * @param id
     * @return
     */
    @Override
    public List<SongMenuEntity> getSongMenuNameBySongMenuId(Integer id) {
        Example example = new Example(SongMenuEntity.class);
        example.createCriteria().andEqualTo("id",id);
        List<SongMenuEntity> select = songMenuMapper.selectByExample(example);
        return select;
    }

    /**
    * @Description 根据类别获取歌单
    * @Author lzy
    * @Date 2022/9/15 9:51
    * @Return java.util.List<com.bgs.music.songMenu.entity.SongMenuEntity>
    * @param typeId
    */
    @Override
    public List<SongMenuEntity> getSongMenuByType(Integer typeId) {
        SongMenuToTypesEntity songMenuToTypesEntity = new SongMenuToTypesEntity();
        songMenuToTypesEntity.setTId(typeId);
        List<SongMenuToTypesEntity> songMenuToTypesEntities = songMenuToTypesMapper.select(songMenuToTypesEntity);
        Example example = new Example(SongMenuEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (typeId != 0) {
            if (songMenuToTypesEntities.size() > 0) {
                for (SongMenuToTypesEntity menuToTypesEntity : songMenuToTypesEntities) {
                    criteria.orEqualTo("id", menuToTypesEntity.getSId());
                }
            } else {
                return null;
            }
        }
        criteria.andEqualTo("delFlag", 1);
        return songMenuMapper.selectByExample(example);
    }

}
